<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchConfig"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="wmsOrderCheckDiffTable"
      >
      </ele-page>
    </div>
    <ele-form-dialog
      class="dialog-pack"
      :isLoading="isLoading"
      v-model="formDataDialog"
      v-bind="formDescDialog"
      :request-fn="handleSubmit"
      :visible.sync="visible"
      :title="title"
      width="550px"
    >
    </ele-form-dialog>
  </div>
</template>

<script>
import { operateList, searchConfig, formDescDialog } from "./config";
import {
  getList,
  getDiffStatusList,
  getDiffTypeList,
  handleDiff,
  getCloseFlagList,
} from "@/api/checkorder/mesOrderCheckDiff";
import { getDict } from "@/api/common";
import {
  getAreaCodeList,
  getCurrentWarehouseCode,
} from "@/api/outbound/wmsOutTaskDetail";
import { hasDuplicateField } from "@/utils/judge";
export default {
  name: "mesOrderCheckDiff",

  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, //新增修改框
      searchConfig,
      operateList,
      getList,

      // 列表页字段的绑定配置
      column: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: $i18n.t("common.order"),
          fixed: "left",
          width: 60,
        },
        {
          prop: "orderNo",
          width: 180,
          fixed: "left",
          showOverflowTooltip: true,
          label: $i18n.t("单据编号"), // 单据编号
        },
        // {
        //   prop: "orderLine",
        //   label: $i18n.t("wmsOrderCheckDiff.orderLine"), // 行项目
        //   width: 100,
        //   showOverflowTooltip: true,
        // },
        {
          prop: "diffStatus",
          width: 120,
          showOverflowTooltip: true,
          label: $i18n.t("差异状态"), // 差异状态
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict("wms_pd_diff_status");
            return list.data.result;
          },
        },
        {
          prop: "closeFlag",
          width: 120,
          showOverflowTooltip: true,
          label: $i18n.t("是否处理"), // 差异状态
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getCloseFlagList();
            return list.data.result;
          },
        },
        {
          prop: "goodsCode",
          width: 160,
          showOverflowTooltip: true,
          label: $i18n.t("物料编码"), // 物料编码
        },
        {
          prop: "goodsName",
          width: 180,
          showOverflowTooltip: true,
          label: $i18n.t("物料名称"), // 物料名称
        },
        {
          prop: "goodsBrand",
          width: 180,
          showOverflowTooltip: true,
          label:"商品品牌", //
        },
        {
          prop: "specs",
          width: 180,
          showOverflowTooltip: true,
          label:"商品规格", //
        },
        {
          prop: "supplierName",
          width: 180,
          showOverflowTooltip: true,
          label:"供应商", //
        },
        {
          prop: "inventoryAmount",
          label: $i18n.t("库存数量"), // 库存数量
          width: 120,
          showOverflowTooltip: true,
        },
        {
          prop: "realAmount",
          label: $i18n.t("实盘数量"), // 实盘数量
          width: 120,
          showOverflowTooltip: true,
        },

        {
          prop: "diffAmount",
          label: $i18n.t("差异数量"), // 差异数量
          width: 120,
          showOverflowTooltip: true,
        },
        {
          prop: "unit",
          label: $i18n.t("common.unit"),
          width: 100,
          showOverflowTooltip: true,
        },
        {
          prop: "warehouseCode",
          label: $i18n.t("common.warehouseName"), // 仓库编码
          width: 180,
          showOverflowTooltip: true,
          optionProp: {
            value: "modelingCode",
            label: "modelingName",
          },
          option: async (data) => {
            const list = await getCurrentWarehouseCode();
            return list.data.result;
          },
        },
        // {
        //   prop: "areaCode",
        //   width: 160,
        //   showOverflowTooltip: true,
        //   optionProp: {
        //     value: "areaCode",
        //     label: "areaDesc",
        //   },
        //   option: async (data) => {
        //     const list = await getAreaCodeList();
        //     return list.data.result;
        //   },
        //   label: $i18n.t("common.area"), // 库区
        // },
        // {
        //   prop: "locationCode",
        //   label: $i18n.t("common.location"), // 库位编码
        //   width: 120,
        //   showOverflowTooltip: true,
        // },
        {
          prop: "handlingMethod",
          label: $i18n.t("处理方式"), // 处理方式
          optionProp: {
            value: "value",
            label: "description",
          },
          width: 150,
          showOverflowTooltip: true,
          option: async (data) => {
            const list = await getDict("wms_pd_handling_method");
            return list.data.result;
          },
        },
        {
          prop: "diffType",
          label: $i18n.t("差异原因"), // 差异原因
          optionProp: {
            value: "value",
            label: "description",
          },
          width: 150,
          showOverflowTooltip: true,
          option: async (data) => {
            const list = await getDict("wms_pd_diff_type");
            return list.data.result;
          },
        },
        {
          prop: "diffDesc",
          width: 150,
          showOverflowTooltip: true,
          label: $i18n.t("原因描述"), // 原因描述
        },
        {
          prop: "processingTime",
          label: $i18n.t("处理时间"), // 处理时间
          width: 160,
          showOverflowTooltip: true,
        },

        {
          prop: "createByName",
          label: $i18n.t("创建人名称"), // 创建人名称
          width: 130,
          showOverflowTooltip: true,
        },
        {
          prop: "createTime",
          label: $i18n.t("创建时间"), // 创建时间
          width: 160,
          showOverflowTooltip: true,
        },
      ],
    };
  },
  computed: {},
  methods: {
    //表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name;
      const fnc = {
        handle: () => {
          if (!hasDuplicateField(data.rows, "orderNo")) {
            this.$message.error($i18n.t('所选项【盘点单号不一致】,不可操作！'));
            return;
          }
          if (data.rows.some((i) => i.closeFlag == "1")) {
            this.$message.error($i18n.t('所选项【已处理】,不可操作！'));
            return;
          }
          if (data.rows.some((i) => i.diffStatus == "10")) {
            this.$message.error($i18n.t('所选项【差异状态=无差异】,不可操作！'));
            return;
          }
          this.formDataDialog = {
            handlingMethod: "10",
            mesOrderCheckDiffList: data.rows.map((i) => {
              return {
                id: i.id,
                orderNo: i.orderNo,
              };
            }),
          };
          this.disableFields(false);
          if (
            data.rows.every(
              (i) => Number(i.numberOfResets) - Number(i.currentCount) == 0
            )
          ) {
            this.formDescDialog.formDesc.handlingMethod.disabled = true;
            this.formDataDialog.handlingMethod = "20";
          } else {
            this.formDescDialog.formDesc.handlingMethod.disabled = false;
          }

          this.visible = true;
        },
      };
      fnc[data.handleName]();
    },

    doSubmit({ title, api, data }) {
      console.log(data);
      this.isLoading = true;
      api(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || title);
          this.visible = false;
          this.refresh();
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return; // 判断状态
      this.doSubmit({
        title: $i18n.t("common.operateSuccess"),
        api: handleDiff,
        data,
      });
    },
    disableFields(flag) {
      this.formDescDialog.disabled = flag;
      this.formDescDialog.isShowSubmitBtn = !flag;
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset);
    },
  },
};
</script>
